/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh. ......,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....  用爱成就每一位学生 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.controller;

import com.itheima.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ ConsumerController</dd>
 * <dd>创建时间：  11:39 2020/8/18</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2020/8/18      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("consumer")
@DefaultProperties(defaultFallback = "defaultFallbackMethod") //针对于consumerController所有的默认熔断方法
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("findUserById/{id}")
    //@HystrixCommand(fallbackMethod = "fallBack") //默认熔断方法
    @HystrixCommand
    public User findUserById(@PathVariable Integer id){
        //初版
        //String url = "http://localhost:8080/user/findUserById/" + id;

        /*
            第二版: 通过向注册中心获取服务列表，解决url硬编码问题
         */
  /*      List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        //由于当前user-service服务只有一个实例，所以list大小为1
        ServiceInstance serviceInstance = instances.get(0);
        //获取uri
        String url = serviceInstance.getUri() + "/user/findUserById/" + id;
        */

        /*
            第三版： 使用ribbon+restTemplate调用
         */
        String url = "http://user-service/user/findUserById/" + id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    /*
        针对于单个请求的默认熔断方法：
            1.返回值类型必须与原方法一致
            2.参数列表必须与原方法一致
            3.方法名称自定义
     */
    public User fallBack(Integer id){
        User user = new User();
        user.setId(id);
        user.setNote("服务繁忙，请稍后再试...");
        return user;
    }

    /**
     * 针对于consumerController所有的默认熔断方法
     * 有的是list返回值---》findUserByName
     * 有的是单个对象user---->findUserById
     * 要求：
     *   1.返回值类型必须一致
     *   2.不能有参数列表
     */
    public User defaultFallbackMethod(){
        User user = new User();
        user.setNote("全局服务繁忙，请稍后再试...");
        return user;
    }
    
    /**
     * @Author: maodong
     * @Date: 14:48 2020/8/18
     * @Params []
     * @ReturnType: java.util.List<org.springframework.cloud.client.ServiceInstance>
     * @Description: 通过服务的应用名称获取服务列表
     */
    @GetMapping("getInstanceList")
    public List<ServiceInstance> getInstanceList(){
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        return instances;
    }
}
